﻿Imports System.IO
Imports System.Xml

Public Class 旧数据库

    '关闭窗体
    Private Sub 旧数据库_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        OldBbSForm = Nothing
    End Sub

    '窗体加载
    Private Sub 旧数据库_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Old_dbfileName <> "NotSet" Then
            TextBox1.Text = Old_dbfileName
        Else
            TextBox1.Text = "未设置"
        End If

    End Sub

    '更改数据库路径
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ofd = New OpenFileDialog
        ofd.Filter = "数据库文件|*.mdb"
        ofd.Title = "请选择数据库"
        ofd.InitialDirectory = apps + "\data\"
        Dim r As DialogResult = ofd.ShowDialog
        If r = DialogResult.OK Then
            TextBox1.Text = ofd.FileName
        End If

    End Sub

    '取消按扭
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    '写入设置
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        '设置文件的位置
        Dim fileS As String = apps + "\data\settings.xml"
        'xml文件对象
        Dim xd As New XmlDocument
        Dim Tnode As XmlNode
        If File.Exists(fileS) Then
            '读取文件
            xd.Load(fileS)
            Dim Root As XmlElement = xd.DocumentElement
            '获得子节点
            Dim SetNodes As XmlNodeList = Root.ChildNodes


            For i = 0 To SetNodes.Count - 1
                If SetNodes.Item(i).Name = "OldDBFile" Then
                    Tnode = SetNodes.Item(i)
                    Exit For
                End If
            Next


        Else
            MessageBox.Show("文件丢失，请重新运行程序")
            Exit Sub
        End If

        If TextBox1.Text <> "未设置" And Tnode IsNot Nothing Then
            Tnode.InnerText = TextBox1.Text
            Old_dbConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Tnode.InnerText + ";;Jet OLEDB:Database Password=665752;"
            Old_dbfileName = Tnode.InnerText
        Else
            Tnode.InnerText = "NotSet"
            Old_dbfileName = "NotSet"
        End If
        '保存文件
        xd.Save(fileS)

        '关闭窗体
        Me.Close()

    End Sub

    '请除按扭
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        TextBox1.Text = "未设置"
    End Sub
End Class